package com.example.doer.reading.AppActivities;

import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.ab.activity.AbActivity;
import com.ab.fragment.AbFragment;
import com.example.doer.reading.AppDataModel.AppUser;
import com.example.doer.reading.R;
import com.example.doer.reading.global.MyApplication;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AbActivity {
    ImageView mTvTabBook;
    ImageView mTvTabDisscuss;
    ImageView mTvTabMessage;
    ImageView mTvTabMine;
    public static final String TAG ="tag";
    public static final int TAB_NUM = 4;
    private ImageView[] mTabViews = new ImageView[TAB_NUM];
    private FragmentManager fragmentManager;
    private List<AbFragment> fragmentList = new ArrayList<>();
    private MyApplication application;

    public List<AbFragment> getFragmentList() {
        return fragmentList;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.activity_main);
        application= (MyApplication) getApplication();

        AppUser.getUserFromServer(MainActivity.this);
        //MyApplication myApplication;
        /*AbTitleBar abTitleBar=getTitleBar();
        abTitleBar.setTitleText("MainActivity");
        abTitleBar.setTitleBarBackground(R.drawable.top_bg2);
        abTitleBar.setTitleTextMargin(10, 0, 0, 0);*/
        initView();
    }


    public void initView(){
        mTvTabBook=(ImageView)findViewById(R.id.tv_tab_book);
        mTvTabDisscuss=(ImageView)findViewById(R.id.tv_tab_discuss);
        mTvTabMessage=(ImageView)findViewById(R.id.tv_tab_message);
        mTvTabMine=(ImageView)findViewById(R.id.tv_tab_mine);

        fragmentManager=getFragmentManager();

        mTabViews[0]=mTvTabBook;
        mTabViews[1]=mTvTabDisscuss;
        mTabViews[2]=mTvTabMessage;
        mTabViews[3]=mTvTabMine;

        for (int i=0;i<mTabViews.length;i++){
            addFragment(i);
            final int j =i;
            mTabViews[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (j==3){
                        startActivity(new Intent(MainActivity.this,MyHomeActivity.class));
                    }
                    else {
                        showTab(j);
                    }
                }
            });
        }
        showTab(0);
    }


    public void addFragment(int index){
        AbFragment fragment =null;
        switch (index) {
            case 0:
                fragment = new FragmentOne(getSupportFragmentManager());
                break;
            case 1:
                fragment = new FragmentTwo();
                break;
            case 2:
                fragment = new MessageFragment();
                break;
            case 3:
                fragment = new FragmentFour();
                break;
        }
        fragmentList.add(fragment);
        fragmentManager.beginTransaction().
                add(R.id.realtabcontent,fragment,String.valueOf(index)).
                commitAllowingStateLoss();
        fragmentManager.beginTransaction().
                hide(fragment).commitAllowingStateLoss();

        fragment.setUserVisibleHint(false);
    }

    private void hideAllFragments(AbFragment fragment) {
        for (int i = 0; i < TAB_NUM; i++) {
            AbFragment f = (AbFragment) fragmentManager.findFragmentByTag(String.valueOf(i));

            if (f != null && f.isAdded() && f != fragment) {
                fragmentManager.beginTransaction().hide(f).commitAllowingStateLoss();
            }
        }
    }

    public void showTab(int index){
        AbFragment targetFragment =fragmentList.get(index);
        hideAllFragments(targetFragment);
        fragmentManager.beginTransaction().show(targetFragment).commitAllowingStateLoss();
        targetFragment.setUserVisibleHint(true);
        for (int i = 0; i < TAB_NUM; i++) {
            if (index == i) {
                changeIconToSelect(i,true);

            } else {
                changeIconToSelect(i,false);
            }
        }
    }

    public void changeIconToSelect(int i,boolean isSelect){
        switch (i){
            case 0:
                if (isSelect)
                    mTabViews[0].setImageResource(R.drawable.book_2);
                else
                    mTabViews[0].setImageResource(R.drawable.book_1);
                break;
            case 1:
                if (isSelect)
                    mTabViews[1].setImageResource(R.drawable.topic_2);
                else
                    mTabViews[1].setImageResource(R.drawable.topic_1);
                break;
            case 2:
                if (isSelect)
                    mTabViews[2].setImageResource(R.drawable.message_2);
                else
                    mTabViews[2].setImageResource(R.drawable.message_1);

                break;
            case 3:
                if (isSelect)
                    mTabViews[3].setImageResource(R.drawable.mine_2);
                else
                    mTabViews[3].setImageResource(R.drawable.mine_1);
                break;
        }
    }

}
